<?php
	if (!session_start()) session_start();
	if (!isset($_SESSION['kiemtradangnhapthinguyet']) || $_SESSION['kiemtradangnhapthinguyet']!=1) {
		header('location: ../../../index.php');
		die();
	}
	$table = "sanpham";
	$id = $_GET['id'];
	$kq=$qt->get_by_thutu($table,$id);
	$record=mysql_fetch_assoc($kq);
	if(isset($_POST['ok']))
	{	
		$id=$_POST['id'];
		$images_ = $_POST["images"];
		$count_img=count($images_);
		for($i=0;$i<$count_img;$i++){
			$images=$images.':*:'.$images_[$i];
		}
		$images=substr($images,3);
		
		$solan=$_POST['solan'];
		settype($solan,"int");if($solan==0) $solan=1;
		
		if($solan > 100){
			$solan = 100;
		}
		
		$bien=$_POST;
		unset($bien['ok']);
		unset($bien['images']);
		unset($bien['solan']);
		
		$bien['url_img']=$images;
		$res=$qt->updateObject1($table,$bien,"id='$id'");
		
		unset($bien['id']);
		if($solan > 1){
			$bien['ngay']= getCurrentDayString();
			for($i=1;$i<=$solan;$i++){
				$bien['thutu']=$qt->get_thutu($table);
				$res=$qt->insertObject1($table,$bien);
			}
		}
		
		if($res){
			echo "<script>alert('Sửa dữ liệu thành công !!!');history.go(-2);</script>";
		}
	}
?>				
<script type="text/javascript">
function isTest() {
	if (document.getElementById("ten_vi").value=="") {
		alert("You have not entered enough information.");
		return false;
	}
	if (document.getElementById("ten_en").value=="") {
		alert("You have not entered enough information.");
		return false;
	}
	
    return true;
}
$(document).ready(function() {
	$('#them_hinh').click(function (){
	n=$('.images').length;
	//alert(n);
	$.ajax({
	   url:'ajax_hinh.php',
	   data:'id='+n,
	   cache: false, 
	   success: function(data){ 
	   $('#images_ajax').append(data);
	  	}
		});
	});
	
	$('.xoa').click(function (){
	$(this).parent().parent().html('');
	})
});	
</script>	
            	<div class="dangnhap_title">
                <a href="index.php" style="color:#F8991D;" title="Trang chủ">Trang chủ</a>
                <span style="font-size:9px;">>></span> 
                Sản phẩm
                <span style="font-size:9px;">>></span> 
                <a href="index.php?p=<?php echo $table; ?>_quanly" style="color:#F8991D;">Sản phẩm</a> 
            	</div>
            	<form method="post" action="index.php?p=<?php echo $table; ?>_sua" onSubmit="return isTest();">
            	<input type="hidden" name="id" value="<?php echo $id; ?>" />
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title" style="font-size:22px; color:#0E6BBB; width: 90%;">
                        Vietnamese's Information
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Tên(*)
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="ten_vi" id="ten_vi" style="width:520px; padding:0px;" value="<?php echo $record['ten_vi']; ?>" />
                    </div>
                </div>
				<div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Kích thước
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="kichthuoc_vi" id="kichthuoc_vi" style="width:520px; padding:0px;" value="<?php echo $record['kichthuoc_vi']; ?>" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Màu sắc
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="mausac_vi" id="mausac_vi" style="width:520px; padding:0px;" value="<?php echo $record['mausac_vi']; ?>" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Chất liệu
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="chatlieu_vi" id="chatlieu_vi" style="width:520px; padding:0px;" value="<?php echo $record['chatlieu_vi']; ?>" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title" style="width:710px; padding-bottom:3px;">
                        &nbsp; &nbsp; &nbsp;- Nội dung:                    </div>
              <div class="dangnhap_col_input" style="width:700px; padding-left:15px;">
                        <textarea id="noidung_vi" name="noidung_vi" rows="4" cols="30"><?php echo $record['noidung_vi']; ?></textarea>
                    	<script type="text/javascript">
						var editor = CKEDITOR.replace( 'noidung_vi',{
							uiColor : '#9AB8F3',
							language:'vi',
							skin:'office2003',
							//width:'200',
							filebrowserImageBrowseUrl : 'ckfinder/ckfinder.html?Type=Images',
							filebrowserFlashBrowseUrl : 'ckfinder/ckfinder.html?Type=Flash',
							filebrowserImageUploadUrl :'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
							filebrowserFlashUploadUrl : 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
						});
						</script>
                    </div>
                </div>
                
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title" style="font-size:22px; color:#0E6BBB; width: 90%;">
                        More Information
                    </div>
                </div>
                <script>
					function gettheloaitin() {
	var id = document.getElementById("idmenu").value;
	$("#idroot").load("librarys/incs/getloai.php?table=sanpham_root&loai=idmenu&id="+id);
}
function getcl() {
	var id = document.getElementById("idroot").value;
	$("#idcl").load("librarys/incs/getloai.php?table=sanpham_chungloai&loai=idroot&id="+id);
}
function getl() {
	var id = document.getElementById("idcl").value;
	$("#idloai").load("librarys/incs/getloai.php?table=sanpham_loai&loai=idcl&id="+id);
}
				</script>
                
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Số lần đặt hàng
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="dathang" id="code" style="width:520px; padding:0px;" value="<?php echo $record['dathang']; ?>" />
                    </div>
                </div>
                
                 <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Mã sản phẩm
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="code" id="code" style="width:520px; padding:0px;" value="<?php echo $record['code']; ?>" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Giá(Nhập số)
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="gia" id="gia" style="width:520px; padding:0px;" value="<?php echo $record['gia']; ?>" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Giá khuyến mãi(Nhập số)
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="gia_km" id="gia_km" style="width:520px; padding:0px;" value="<?php echo $record['gia_km']; ?>" />
                    </div>
                </div>
                
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- sản phẩm menu:
                    </div>
                    <div class="dangnhap_col_input">
                    	<select name="idmenu" id="idmenu" onchange="gettheloaitin()">
                    		<option value="0"> - - Chọn - - </option>
							<?php 
                    			
                    			$tinroot= $qt->get_by_thutu('sanpham_menu');
                    			while($row=mysql_fetch_assoc($tinroot)) {
                    				if ($idparent==0) $idparent=$row['id'];
                    		?>
                    		<option value="<?php echo $row['id']; ?>" <? if($row['id']==$record['idmenu']) echo "selected='selected'" ?> ><?php echo $row['ten_vi']; ?></option>
                    		<?php 
                    			}
                    		?>
                    	</select>
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- sản phẩm root:
                    </div>
                    <div class="dangnhap_col_input">
                    	<select name="idroot" id="idroot" onchange="getcl()">
                    		<option value="0"> - - Chọn - - </option>
                           <?php 
						   		$idparent = $record['idmenu'];
                    			$tintheloai = $qt->get_by_thutu("sanpham_root", -1,-1,"and idmenu = '$idparent'");
                    			while($rowtheloai=mysql_fetch_assoc($tintheloai)) {
                    		?>
                    		<option value="<?php echo $rowtheloai['id']; ?>" <? if($rowtheloai['id']==$record['idroot']) echo "selected='selected'" ?> ><?php echo $rowtheloai['ten_vi']; ?></option>
                    		<?php 
                    			}
                    		?>
                    	</select>
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;-Sản phẩm chủng loại:
                    </div>
                    <div class="dangnhap_col_input">
                    	<select name="idcl" id="idcl" onchange="getl()">
                    		<option value="0"> - - Chọn - - </option>
                           <?php 
						   		$idparent = $record['idroot'];
                    			$tintheloai = $qt->get_by_thutu("sanpham_chungloai", -1,-1,"and idroot = '$idparent'");
                    			while($rowtheloai=mysql_fetch_assoc($tintheloai)) {
                    		?>
                    		<option value="<?php echo $rowtheloai['id']; ?>" <? if($rowtheloai['id']==$record['idcl']) echo "selected='selected'" ?> ><?php echo $rowtheloai['ten_vi']; ?></option>
                    		<?php 
                    			}
                    		?>
                    	</select>
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;-Sản phẩm loại:
                    </div>
                    <div class="dangnhap_col_input">
                    	<select name="idloai" id="idloai">
                    		<option value="0"> - - Chọn - - </option>
                           <?php 
						   		$idparent = $record['idcl'];
                    			$tintheloai = $qt->get_by_thutu("sanpham_loai", -1,-1,"and idcl = '$idparent'");
                    			while($rowtheloai=mysql_fetch_assoc($tintheloai)) {
                    		?>
                    		<option value="<?php echo $rowtheloai['id']; ?>" <? if($rowtheloai['id']==$record['idloai']) echo "selected='selected'" ?> ><?php echo $rowtheloai['ten_vi']; ?></option>
                    		<?php 
                    			}
                    		?>
                    	</select>
                    </div>
                </div>
                
                 <div style="float:left;width:100%;margin:4px 0px;height:1px; border-top:1px dashed #333333"></div>
				<div id="images_ajax" class="dangnhap_col">
				<? $images=explode(":*:",$record['url_img']);
				for($i=0;$i<count($images);$i++) {
	  			?>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Images:
                    </div>
                    <div class="dangnhap_col_input">
                        <input id="images<?=$i?>" type="text" name="images[]" style="width:445px; padding:0px;" class="images" value="<?=$images[$i]?>" />
                        <input onclick="BrowseServer('Images:/','images<?=$i?>')" type="button" name="btnChonFile" id="btnChonFile" value="...." />
                        <input type="button"  class="xoa" value="Xóa" />	
                    </div>
                
				</div>
                <? }?>
				</div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title" style="width:710px; padding-bottom:3px;">
                        &nbsp; &nbsp; &nbsp;- Thêm hình:  <input id="them_hinh" value="Thêm hình" type="button" />
                    </div>
   				</div>
				<div style="float:left;width:100%;margin:4px 0px;height:1px; border-top:1px dashed #333333"></div>
                
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Hot:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="radio" name="hot" value="1" <?php if ($record['hot']==1) echo "checked='checked'"; ?> />Bình thường
                     	<input type="radio" name="hot" value="0" <?php if ($record['hot']==0) echo "checked='checked'"; ?> />Hot
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Sản phẩm mới:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="radio" name="trangchu" value="1" <?php if ($record['trangchu']==1) echo "checked='checked'"; ?> />Bình thường
                     	<input type="radio" name="trangchu" value="0" <?php if ($record['trangchu']==0) echo "checked='checked'"; ?> />Trang chủ
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Title
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="title" id="title" style="width:520px; padding:0px;" value="<?php echo $record['title']; ?>" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Keyword
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="keyword" id="keyword" style="width:520px; padding:0px;" value="<?php echo $record['keyword']; ?>" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Description
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="text" name="description" id="description" style="width:520px; padding:0px;" value="<?php echo $record['description']; ?>" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Hidden or Show:
                    </div>
                    <div class="dangnhap_col_input">
                        <input type="radio" name="anhien" value="1" <?php if ($record['anhien']==1) echo "checked='checked'"; ?> />Show
                     	<input type="radio" name="anhien" value="0" <?php if ($record['anhien']==0) echo "checked='checked'"; ?> />Hidden
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_title">
                        &nbsp; &nbsp; &nbsp;- Số lần nhân(> 0)
                    </div>
                    <div class="dangnhap_col_input">
                        <input onkeypress="return isNumberKey(event);" type="text" name="solan" id="solan" value="1" style="width:520px; padding:0px;" />
                    </div>
                </div>
                <div class="dangnhap_col">
                    <div class="dangnhap_col_input">
                        <input type="submit" class="ok" border="0" name="ok" id="ok" value="" />
                        <a href="javascript:history.go(-1);"><img src="imgs/exit_button.png" border="0" /></a>
                    </div>
                </div>
               	</form>